package com.cyl.manager.pms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.cyl.h5.domain.vo.H5ProductVO;
import com.cyl.manager.act.domain.entity.CouponCategory;
import com.cyl.manager.act.service.CouponCategoryService;
import com.cyl.manager.lbs.service.LbsZbDpService;
import com.cyl.manager.oms.domain.query.DpOrderQuery;
import com.cyl.manager.oms.domain.vo.DpOrderVO;
import com.cyl.manager.pms.convert.ProductConvert;
import com.cyl.manager.pms.domain.entity.Product;
import com.cyl.manager.pms.domain.query.ProductQuery;
import com.cyl.manager.pms.service.ProductService;
import com.cyl.manager.pms.domain.vo.ProductVO;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 商品信息Controller
 * 
 * @author zcc
 * @date 2022-11-28
 */
@Api(description ="商品信息接口列表")
@RestController
@RequestMapping("/pms/product")
public class ProductController extends BaseController {
    @Autowired
    private ProductService service;
    @Autowired
    private ProductConvert convert;

    @Autowired
    private LbsZbDpService lbsZbDpService;
    
    @Autowired
    private CouponCategoryService couponCategoryService;
    @ApiOperation("商品销量统计")
    @PreAuthorize("@ss.hasPermi('pms:product:list')")
    @PostMapping("/selectProductXl")
    public ResponseEntity<Integer> selectProductXl(@RequestBody ProductQuery query) {
    	 return ResponseEntity.ok(lbsZbDpService.selectProductXl(query.getId()));
    }
    
    @ApiOperation("商品销售信息统计")
    @PreAuthorize("@ss.hasPermi('pms:product:list')")
    @PostMapping("/selectProductXlTj")
    public ResponseEntity<DpOrderVO> selectProductXlTj(@RequestBody ProductQuery query) {
    	 return ResponseEntity.ok(lbsZbDpService.selectProductXlTj(query.getId()));
    }
    
    
    @ApiOperation("查询商品信息列表")
    @PreAuthorize("@ss.hasPermi('pms:product:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<Product>> list(@RequestBody ProductQuery query, Pageable page) {
        Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        List<Product> list = service.selectList(query, page);
        if(list!=null && list.size()>0) {
        	for (Product product : list) {
        		 if(product.getCplx()!=null && product.getDhCouponCategoryId()!=null) {
                 	CouponCategory couponCategory = couponCategoryService.selectCheckById(product.getDhCouponCategoryId());
                 	if(couponCategory!=null) {
                 		 Map<String, Object> params = product.getParams();
                 		 if(params==null) {
                 			params = new HashMap<String, Object>();
                 		 }
                 		params.put("flqmc", couponCategory.getTitle());
                 		product.setParams(params);
                 	}
                 }
			}
        }
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }

    @ApiOperation("导出商品信息列表")
    @PreAuthorize("@ss.hasPermi('pms:product:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(ProductQuery query) {
        Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        List<Product> list = service.selectList(query, null);
        ExcelUtil<ProductVO> util = new ExcelUtil<>(ProductVO.class);
        return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "商品信息数据"));
    }

    @ApiOperation("获取商品信息详细信息")
    @PreAuthorize("@ss.hasPermi('pms:product:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<ProductVO> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("新增商品信息")
    @PreAuthorize("@ss.hasPermi('pms:product:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody ProductVO product) {
        return ResponseEntity.ok(service.insert(product));
    }
    
    @ApiOperation("新增商品信息福利券商品")
    @PreAuthorize("@ss.hasPermi('pms:product:addFlqsp')")
    @Log(title = "商品信息福利券商品", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addFlqsp")
    public ResponseEntity<Integer> addFlqsp(@RequestBody ProductVO product) {
        return ResponseEntity.ok(service.insert(product));
    }
    
    @ApiOperation("新增商品信息积分商品")
    @PreAuthorize("@ss.hasPermi('pms:product:addJf')")
    @Log(title = "商品信息积分商品", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addJf")
    public ResponseEntity<Integer> addJf(@RequestBody ProductVO product) {
        return ResponseEntity.ok(service.insert(product));
    }
    
    @ApiOperation("修改商品信息")
    @PreAuthorize("@ss.hasPermi('pms:product:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody ProductVO product) {
        return ResponseEntity.ok(service.update(product));
    }

    
    @ApiOperation("商品上下架")
    @PreAuthorize("@ss.hasPermi('pms:product:edit')")
    @Log(title = "商品上下架", businessType = BusinessType.UPDATE)
    @PutMapping("/spsxj")  ///pms/product/spsxj 参数 id publishStatus
    public ResponseEntity<Integer> spsxj(@RequestBody ProductVO product) {
        return ResponseEntity.ok(service.spsxj(product));
    }
    
    
    @ApiOperation("删除商品信息")
    @PreAuthorize("@ss.hasPermi('pms:product:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public ResponseEntity<Integer> remove(@PathVariable Long id) {
        return ResponseEntity.ok(service.deleteById(id));
    }
    
    @ApiOperation("删除商品信息")
    @PreAuthorize("@ss.hasPermi('pms:product:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("hsz/{id}")
    public ResponseEntity<Integer> removehsz(@PathVariable Long id) {
        return ResponseEntity.ok(service.deleteHszById(id));
    }
    
}
